﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace mVu
{
  class xHamster : Site
  {
    public override Dictionary<string, string> GetCategories()
    {
      var categories = new Dictionary<string, string>
                         {
                           {String.Empty, String.Empty}
                         };

      return categories;
    }

    public override Dictionary<string, string> GetSorts()
    {
      var sorts = new Dictionary<string, string>
                    {
                      {String.Empty, String.Empty},
                      {"Relevance", String.Empty},
                      {"Date Added", "s=da"},
                      {"View Count", "s=vc"},
                      {"Rating", "s=rt"}
                    };
      return sorts;
    }

    public override Dictionary<string, string> GetMediaTypes()
    {
      var types = new Dictionary<string, string>
                    {
                      {"FLV", "FLV"}
                    };
      return types;
    }

    public override List<Thumbnail> GetThumbnails(string baseAddress, string sort, string query, int page)
    {
      var list = new List<Thumbnail>();

      try
      {
        if (String.IsNullOrEmpty(baseAddress))
        {
          baseAddress = String.Empty;
        }

        var address = String.Format("http://www.xhamster.com");
        address = address + (String.IsNullOrEmpty(query) ? String.Empty : String.Format("/search.php?q={0}", query));
        address = address + (String.IsNullOrEmpty(query) ? "?" : "&");
        address = address + (String.IsNullOrEmpty(sort) ? String.Empty : String.Format("{0}", sort));
        address = address + (String.IsNullOrEmpty(query) && String.IsNullOrEmpty(sort) ? "?" : "&"); 
        address = address + (String.Format("page={0}", page));
        var result = GetPageSource(address);

        if (!String.IsNullOrEmpty(result))
        {
          var rh = new SgmlReaderDll.SGMLReaderHelper();
          var xhtml = rh.ProcessString(result);

          var doc = XDocument.Parse(xhtml);

          list.AddRange(from node in doc.Descendants("td")
                        where node.Attribute("width") != null && node.Attribute("width").Value.Equals("166px")
                        select new Thumbnail
                        {
                          Image = node.Descendants("img").First().Attribute("src").Value,
                          Url = String.Format("http://xhamster.com/{0}", node.Descendants("a").Where(n => n.Attribute("class") != null && n.Attribute("class").Value == "hRotator").First().Attribute("href").Value),
                          Title = node.Descendants("div").Where(n => n.Attribute("class") != null && n.Attribute("class").Value == "moduleFeaturedTitle").Descendants("a").First().Value,
                          Length = String.Format("({0})", node.Descendants("div").Where(n => n.Attribute("class") != null && n.Attribute("class").Value == "moduleFeaturedDetails").Select(n => n.Value).First().Replace("Views", " - Views").Replace("just added", " - just added"))
                        });
        }
      }
      catch (Exception ex)
      {
        Utilities.HandleError(ex);
      }

      return list;
    }

    public override Dictionary<string, string> GetVideoPaths(string url)
    {
      var dictionary = new Dictionary<string, string>();

      try
      {
        if (!String.IsNullOrEmpty(url))
        {
          var result = GetPageSource(url);

          if (!String.IsNullOrEmpty(result))
          {
            var start = result.IndexOf("'file': '") + 9;
            var end = result.IndexOf("'", start);

            dictionary.Add("FLV", String.Format("http://xhamster.com/flv2/{0}", result.Substring(start, end - start)));
          }
        }
      }
      catch (Exception ex)
      {
        Utilities.HandleError(ex);
      }

      return dictionary;
    }
  }
}
